package com.architecture.util.date;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;

import com.architecture.util.object.ObjectUtility;
import com.architecture.util.string.StringUtility;

/**
 * <p>This class has utilities methods for be used with Dates.</p>
 * @author JR
 * @version 1.0
 * @category Date Utilities
 */
public final class DateUtility {
	
	/**
	 * private constructor of <code>DateUtility</code>.
	 */
	private DateUtility() {
		super();
	}
	
	/**
	 * <p>Format a date using the default locale</p>
	 * @param date
	 * @return String
	 */
	public static String toDate(final Date date){
		if(ObjectUtility.isNotRef(date)){
			return DateFormat.getInstance().format(date);
		}else{
			return StringUtility.EMPTY_STRING;
		}
	}
	
	/**
	 * <p>Returns the current date.</p>
	 * @return Date
	 */
	public static Date date(){
		return new Date();
	}
	
	/**
	 * <p>Extract a year of a date.</p>
	 * @param date
	 * @return int
	 */
	public static int year(final Date date){
		if(ObjectUtility.isNotRef(date)){
			final Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			return calendar.get(Calendar.YEAR);
		}else{
			return 0;
		}
	}
	
	/**
	 * <p>Extract a month of a date.</p>
	 * @param date
	 * @return int
	 */
	public static int month(final Date date){
		if(ObjectUtility.isNotRef(date)){
			final Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			return calendar.get(Calendar.MONTH);
		}else{
			return 0;
		}
	}
	
	/**
	 * <p>Extract a day of a date.</p>
	 * @param date
	 * @return int
	 */
	public static int day(final Date date){
		if(ObjectUtility.isNotRef(date)){
			final Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			return calendar.get(Calendar.DAY_OF_MONTH);
		}else{
			return 0;
		}
	}
	
	/**
	 * <p>Verify if two dates are equals.</p>
	 * @param arg0
	 * @param arg1
	 * @return Boolean
	 */
	public static Boolean eq(final Date arg0, final Date arg1){
		if(ObjectUtility.isAllRef(arg0,arg1)){
			return arg0.getTime() == arg1.getTime();
		}else{
			return Boolean.FALSE;
		}
	}
	
	/**
	 * <p>Verify if the first date is great than second date.</p>
	 * @param arg0
	 * @param arg1
	 * @return Boolean
	 */
	public static Boolean gt(final Date arg0, final Date arg1){
		if(ObjectUtility.isAllRef(arg0,arg1)){
			return arg0.after(arg1);
		}else{
			return Boolean.FALSE;
		}
	}
	
	/**
	 * <p>Verify if the first date is less than second date.</p>
	 * @param arg0
	 * @param arg1
	 * @return Boolean
	 */
	public static Boolean lt(final Date arg0, final Date arg1){
		if(ObjectUtility.isAllRef(arg0,arg1)){
			return arg0.before(arg1);
		}else{
			return Boolean.FALSE;
		}
	}
	
}
